package com.hmys.api.controller;

import com.github.pagehelper.PageInfo;
import com.hmys.api.config.Constant;
import com.hmys.api.model.WebHouse;
import com.hmys.api.model.WebUser;
import com.hmys.api.response.Result;
import com.hmys.api.response.ResultCode;
import com.hmys.api.response.ResultPage;
import com.hmys.api.service.WebHouseCollectionService;
import com.hmys.api.service.WebHouseService;
import com.hmys.api.service.WebUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * Created by chen365 on 2018/03/29.
 * 房源收藏
 */
@RestController
@Api(tags = "收藏")
@RequestMapping(value = "/collection")
public class CollectionController {
    @Autowired
    private WebHouseCollectionService webHouseCollectionService;

    @Autowired
    private WebUserService webUserService;

    @Autowired
    private WebHouseService webHouseService;

    @ApiOperation(value = "收藏", notes = "收藏", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "houseId", value = "房源Id", required = true, paramType = "path")
    })
    @PostMapping(value = "/house/{houseId}")
    public Object collectionHouse(@PathVariable("houseId") Long houseId) {
        WebUser webUser = webUserService.getCurrentUser();
        if (webUser == null) {
            return new Result(ResultCode.LOGIN_TIMEOUT);
        }
        if (StringUtils.isEmpty(houseId)) {
            return new Result(ResultCode.ERROR_PARAM);
        }
        if (webHouseCollectionService.save(webUser.getUserId(), houseId) > 0) {
            return new Result(ResultCode.SUCCESS);
        }
        return new Result(ResultCode.FAIL);
    }

    @ApiOperation(value = "我的收藏", notes = "我的收藏", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "pageIndex", defaultValue = "1", value = "页码", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", defaultValue = "10", value = "每页数", required = true, paramType = "path")
    })
    @GetMapping(value = "/list/{pageIndex}/{pageSize}")
    public Object list(@PathVariable("pageIndex") Integer pageIndex,
                       @PathVariable("pageSize") Integer pageSize) {
        WebUser webUser = webUserService.getCurrentUser();
        if (webUser == null) {
            return new Result(ResultCode.LOGIN_TIMEOUT);
        }
        PageInfo<WebHouse> pageInfo = webHouseService.selectByCollection(pageIndex, pageSize, webUser.getUserId());
        if (pageInfo.getList().size() > 0) {
            return new ResultPage(ResultCode.SUCCESS, pageInfo);
        } else {
            return new ResultPage(ResultCode.NOT_FOUND);
        }
    }

    @ApiOperation(value = "我的收藏(houseIds)", notes = "我的收藏(houseIds)", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/houseIds")
    public Object houseIds() {
        WebUser webUser = webUserService.getCurrentUser();
        if (webUser == null) {
            return new Result(ResultCode.LOGIN_TIMEOUT);
        }
        List<Long> list = webHouseCollectionService.selectHouseIdsByUserId(webUser.getUserId());
        if (list.size() > 0) {
            return new Result(ResultCode.SUCCESS, list);
        } else {
            return new Result(ResultCode.NOT_FOUND);
        }
    }
}
